﻿using AntdUI;
using Microsoft.IdentityModel.Tokens;
using SqlSugar.Extensions;

namespace DataAcquisition
{
    public partial class DevicePointAddForm : BaseForm
    {
        #region 属性

        /// <summary>点位
        /// 
        /// </summary>
        private DevicePoint obj = new DevicePoint();

        /// <summary>设备
        /// 
        /// </summary>
        public DeviceItem deviceItem;

        #endregion

        #region 自定义方法

        /// <summary>重载
        /// 
        /// </summary>
        private void Reload()
        {
            PointNumberInput.Text = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
            PointNameInput.Text = "";
            PointAddressInput.Text = "";
            RangeMinInput.Text = "";
            RangeMaxInput.Text = "";
            OutMinInput.Text = "";
            OutMaxInput.Text = "";
            AmendInput.Text = "";
            AlarmHightInput.Text = "";
            AlarmLowInput.Text = "";
            ErrorRangeInput.Text = "";
            UnitInput.Text = "";
            RegisterTypeSelect.Text = "";
        }
        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["device_number"] = DeviceNumberInput.Text;
            where["name"] = PointNameInput.Text;
            List<DevicePoint> list = SqlHelper.Inquireable<DevicePoint>(where);
            int count = list.Count;
            if (count > 0)
            {
                string msg = string.Format("【{0}】设备已存在【{1}】点位", deviceItem.Name, where["name"]);
                Error(msg);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary> 查询设备
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private Device? InquireDevice(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = number;
            List<Device> list = SqlHelper.Inquireable<Device>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        private int RegisterTypeToInt(string typeString)
        {
            int type = 0;
            if (typeString == "DI")
            {
                type = 1;
            }
            else if (typeString == "DO")
            {
                type = 2;
            }
            else if (typeString == "Input")
            {
                type = 3;
            }
            else if (typeString == "Holding")
            {
                type = 4;
            }
            return type;
        }
        #endregion

        #region 系统方法
        public DevicePointAddForm()
        {
            InitializeComponent();
            Header.Text = "添加设备点位";
        }

        private void DevicePointAddForm_Load(object sender, EventArgs e)
        {
            Reload();
            if (deviceItem is not null)
            {
                Device? device = InquireDevice(deviceItem.Number);
                if (device is not  null)
                {
                    if (device.Name is not null)
                    {
                        DeviceNameInput.Text = device.Name;
                    }

                    DeviceNumberInput.Text = device.Number.ToString();
                    DeviceAddressInput.Text = device.Address.ToString();
                }
                else
                {
                    if (this.Owner is not null)
                    {
                        DevicePointManageForm frm = (DevicePointManageForm)this.Owner;
                        frm.LoadList();
                        string msg = string.Format("【{0}】设备不存在，无法添加点位", deviceItem.Name);
                        frm.Error(msg);
                        this.Close();
                    }
                }
            }
            else
            {
                this.Close();
            }
            GroupBox3.Visible = false;
        }
        #endregion

        #region 响应事件
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (PointNameInput.Text.Length == 0)
            {
                Error("请输入点位名称");
                PointNameInput.Focus();
                return;
            }

            if (PointAddressInput.Text.Length == 0)
            {
                Error("请输入点位地址");
                PointAddressInput.Focus();
                return;
            }
            obj.Address = PointAddressInput.Text.ObjToInt();

            if (RegisterTypeSelect.Text.Length == 0)
            {
                Error("请选择存储类型");
                RegisterTypeSelect.Focus();
                return;
            }

            bool repeatRes = CheckRepeat();
            if (repeatRes)
            {
                return;
            }
            obj.Number = PointNumberInput.Text.ObjToInt();
            obj.Name = PointNameInput.Text;
            obj.Address = PointAddressInput.Text.ObjToInt();
            obj.PointType = RegisterTypeToInt(RegisterTypeSelect.Text);
            if (!RangeMaxInput.Text.IsNullOrEmpty())
            {
                obj.RangeMax = Math.Round(double.Parse(RangeMaxInput.Text), 3);
            }
            if (!RangeMinInput.Text.IsNullOrEmpty())
            {
                obj.RangeMin = Math.Round(double.Parse(RangeMinInput.Text), 3);
            }
            if (!OutMaxInput.Text.IsNullOrEmpty())
            {
                obj.OutMax = Math.Round(double.Parse(OutMaxInput.Text), 3);
            }
            if (!OutMinInput.Text.IsNullOrEmpty())
            {
                obj.OutMin = Math.Round(double.Parse(OutMinInput.Text), 3);
            }
            if (!AmendInput.Text.IsNullOrEmpty())
            {
                obj.Amend = Math.Round(double.Parse(AmendInput.Text), 3);
            }
            if (!AlarmHightInput.Text.IsNullOrEmpty())
            {
                obj.AlarmHight = Math.Round(double.Parse(AlarmHightInput.Text), 3);
            }
            if (!AlarmLowInput.Text.IsNullOrEmpty())
            {
                obj.AlarmLow = Math.Round(double.Parse(AlarmLowInput.Text), 3);
            }
            if (!ErrorRangeInput.Text.IsNullOrEmpty())
            {
                obj.ErrorRange = Math.Round(double.Parse(ErrorRangeInput.Text), 3);
            }
            obj.Unit = UnitInput.Text;
            obj.DeviceNumber = DeviceNumberInput.Text.ObjToInt();
            obj.DeviceName = DeviceNameInput.Text;
            obj.DeviceAddress = DeviceAddressInput.Text.ObjToInt();
            List<DevicePoint> list = new List<DevicePoint>();
            list.Add(obj);
            int res = SqlHelper.Insertable(list);
            if (res > 0)
            {
                Reload();
                if (this.Owner is not null)
                {
                    DevicePointManageForm frm = (DevicePointManageForm)Owner;
                    frm.LoadList();
                }
                this.Close();
            }
            else
            {
                Error("添加点位失败");
            }
        }

        private void RegisterTypeSelect_SelectedValueChanged(object sender, ObjectNEventArgs e)
        {
            if (RegisterTypeSelect.Text == "Input" || RegisterTypeSelect.Text == "Holding")
            {
                GroupBox3.Enabled = true;
                GroupBox3.Visible = true;
            }
            else
            {
                GroupBox3.Enabled = false;
                GroupBox3.Visible = false;
            }
        }

        #endregion
    }
}
